<?php


class search_processor_l_user
{
    /**
     * Sets the limit for the pagination, i.e number of rows in a page.
     */
    const limit = 5;
    
    /**
     * Counts users found under the search
     *
     * @param   string      username
     * @param   string      email
     * @param   string      country
     * @param   string      province
     * @return  int         number of user found
     */
    public function count_users(array $data) //$fname, $lname, $username, $email, $country, $prov
    {
        $user = new user_m_user();
        $user->like("first_name", $data['fname']);
        $user->like("last_name", $data['lname']);
        $user->like("username", $data['username']);
        $user->like("email", $data['email']);

        return $user->count();
    }

    public function list_product(array $data, $offset, $num_records, $sort_column, $order, $filter)
    {
        $list = array();
        $filtered_list = array();
        $queue = array(
            0 => "id",
            1 => "first_name",
            2 => "last_name",
            3 => "username",
            4 => "email"
        );

        $query = $this->search_users($data, $offset, $num_records, $queue[$sort_column], $order, $filter);//$data["start_index"], $data["num_records"], $data["sort_column"], $data["sort_dir"], $data["search_dir"]);

        foreach($query as $row){
                $action = "<a href='".base_url()."index.php/user/admin/user_detail/load_user_details/".$row->$queue[0]."'>
                                <img src='".base_url()."application/modules/core/images/common/detail.png' title='Details'  />
                           </a>";
                $list[] = array($row->$queue[1], $row->$queue[2], $row->$queue[3], $row->$queue[4], $action);
        }

        return $list;
    }

    public function autocomplete(array $data)
    {
        $list ="";
        $query = $this->search_users($data);

        foreach($query as $row){
            $list = $list." ".$row->username;
        }

        return $list;
    }
    /**
     * Counts users found under the search
     *
     * @param   string      username
     * @param   string      email
     * @param   string      country
     * @param   string      province
     * @param   int         offset
     * @return  object         user found
     */
    public function search_users(array $data, $offset=0, $num_of_records=10, $column="first_name", $order="ASC", $filter="")
    {
        $user = new user_m_user();

        $user->query('   SELECT *
                        FROM users
                        WHERE ( first_name LIKE \'%'.$data['fname'].'%\' AND last_name LIKE \'%'.$data['lname'].'%\' AND username LIKE \'%'.$data['username'].'%\' AND email LIKE \'%'.$data['email'].'%\')
                                AND
                              ( first_name LIKE \'%'.$filter.'%\' OR last_name LIKE \'%'.$filter.'%\' OR username LIKE \'%'.$filter.'%\' OR email LIKE \'%'.$filter.'%\')
                        ORDER BY '.$column.' '.$order.'
                        LIMIT '.$offset.', '.$num_of_records.' ');

        return $user;
    }

    /**
     * Returns the limit for number of records in one page (pagination)
     *
     * @param   none
     * @return  const         limit
     */
    public function get_limit()
    {
        return self::limit;
    }
}
?>
